package com.ww.controller;

import com.github.pagehelper.PageInfo;
import com.ww.common.R;
import com.ww.common.SendEmail;
import com.ww.pojo.User;
import com.ww.pojo.UserVO;
import com.ww.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.HttpCookie;
import java.security.GeneralSecurityException;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    //注入UserService对象
    @Autowired
    private UserService userService;

    /**
     * 登录功能
     * @param user
     * @param httpsession
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public R login(@RequestBody User user, HttpSession httpsession){
        user = userService.login(user.getUser_email(),user.getUser_password());
        System.out.println("user = " + user);
        if(user!=null){
            httpsession.setAttribute("userId",user.getUser_id());
            httpsession.setAttribute("userName",user.getUser_name());
            httpsession.setAttribute("userEmail",user.getUser_email());
            httpsession.setAttribute("Role",user.getUser_role());
            return R.builder().code(R.CODE_SUCCESS).message("登录成功").build();
        }
        return R.builder().code(R.CODE_ERROR).message("登录失败").build();
    }

    /**
     * 注册模块
     * 1.检验邮箱是否存在
     * @param uservo
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryemail")
    public R queryemail(@RequestBody UserVO uservo){
        User user = userService.queryemail(uservo);
        if(user==null)return R.builder().code(R.CODE_SUCCESS).message("验证成功").build();
        return R.builder().code(R.CODE_ERROR).message("邮箱已存在").build();
    }

    /**
     * 注册模块
     * 2.发送验证码(邮箱)
     * @param uservo
     * @return
     */
    @ResponseBody
    @RequestMapping("/sendemail")
    public R sendemail(@RequestBody UserVO uservo){
        try {
            SendEmail.send(uservo.getVerification_code(),uservo.getUser_email());
            return R.builder().code(R.CODE_SUCCESS).message("发送成功").build();
        } catch (MessagingException | GeneralSecurityException e) {
            System.out.println("发送失败");
            e.printStackTrace();
        }
        return R.builder().code(R.CODE_ERROR).message("发送失败").build();
    }

    /**
     * 注册模块
     * 3.插入用户数据
     * @param uservo
     * @return
     */
    @ResponseBody
    @RequestMapping("/adduser")
    public R adduser(@RequestBody UserVO uservo){
        int i = userService.adduser(uservo);

        if(i>0)return R.builder().code(R.CODE_SUCCESS).message("注册成功").build();
        return R.builder().code(R.CODE_ERROR).message("注册失败").build();
    }

    /**
     * 查找所有用户
     * @return
     */
    @RequestMapping("/find")
    public String findalluser1(Model model){
        List<User> userList = userService.findalluser();
        System.out.println("userList = " + userList);
        model.addAttribute("user",userList);
        return "admin/user";
    }

    /**
     * 分页查询
     * @param setPageNum
     * @return
     */
    @RequestMapping(value = "/userpage" )
    public String userpage(@RequestParam(value = "setPageNum",defaultValue = "1") String setPageNum, Model model){
        PageInfo<User> userpage=userService.userpage(Integer.valueOf(setPageNum),3);
        model.addAttribute("userpage",userpage);
        return "admin/examples/user";
    }

    /**
     * 通过用户id进行删除操作
     * @param user_id
     * @return
     */
    @RequestMapping("/deleteuser")
    public String deleteuser(@RequestParam(value = "id") String user_id, Model model){
        System.out.println("删除user_id = " + user_id +"的用户");
        Boolean b = userService.deleteuser(Integer.valueOf(user_id));
//        if(b==true){
//            return R.success();
//        }
//        return R.fail();
        PageInfo<User> userpage=userService.userpage(1,3);
        model.addAttribute("userpage",userpage);
        System.out.println("能用");
        return "admin/examples/user";
    }

    @RequestMapping("checklogin")
    @ResponseBody
    public R checklogin(HttpSession httpSession){
        if (httpSession.getAttribute("userId") != null) {
            if (httpSession.getAttribute("Role").equals("admin")||httpSession.getAttribute("Role").equals("ADMIN")){
                return R.builder().code(R.CODE_ADMIN).message("管理员").build();
            }
            return  R.builder().code(R.CODE_SUCCESS).message("普通用户").build();
        }
        return  R.builder().code(R.CODE_ERROR).message("登录失败").build();
    }

    @RequestMapping("/registertest")
    @ResponseBody
    public List<User> registertest(@RequestBody UserVO uservo){
        System.out.println(uservo);
        List<User> userList = userService.findalluser();
        //System.out.println("userList = " + userList);
        return userList;
    }

    @RequestMapping("/loginout")
    public String loginout(HttpSession session){
        session.invalidate();
        return "index";
    }


    @GetMapping("/test")
    public String model(HttpServletRequest request){
        HttpSession session = request.getSession(true);
        session.setAttribute("name","王哥");
        session.setAttribute("password","王哥的密码");
        System.out.println("能用");
        System.out.println("session = " + session);
        return "admin/examples/dashboard";
    }

    @GetMapping("/test1")
    public String model1(HttpServletRequest request,HttpSession session){
//        HttpSession session = request.getSession(true);
        session.setAttribute("name","王哥");
        session.setAttribute("password","王哥的密码");
        System.out.println("能用");
        return "test";
    }

}
